package nl.hanze.designpatterns.mvc.view;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import nl.hanze.designpatterns.domain.TroubleTicket;
import nl.hanze.designpatterns.mvc.controller.TroubleTicketController;
import nl.hanze.designpatterns.mvc.model.TroubleTicketModel;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;


public class TroubleTicketSelectedView extends View<TroubleTicketController, TroubleTicketModel> {

	private static final long serialVersionUID = 1L;
		
	private JTextField userName;
	private JTextField caller;
	private JTextArea description;
	private JLabel date;
	private JButton submitButton;
	
	
	public TroubleTicketSelectedView(TroubleTicketController controller, TroubleTicketModel model) {
		super(controller, model);
		this.init(controller);
		this.setBorder(BorderFactory.createTitledBorder("Selected Ticket"));
		
		FormLayout layout = new FormLayout(
				"right:pref, 2dlu,  20dlu, 3dlu, 100dlu, 30dlu, 50dlu, 8dlu, 100dlu",
				"p, 5dlu, p, 6dlu, p, 10dlu, p, 5dlu, p, 5dlu, p, 5dlu, p, 5dlu, p, 5dlu, p, 5dlu , p");
		
		PanelBuilder builder = new PanelBuilder(layout);
		
		CellConstraints cc = new CellConstraints();
				
		builder.addLabel("UserName: ", 					cc.xy(1, 11));
		builder.addLabel("Caller: ", 					cc.xy(1, 13));
		builder.addLabel("Date: ",	   					cc.xy(1, 15));		
		builder.add(this.userName,						cc.xyw(4, 11, 2));
		builder.add(this.caller,						cc.xyw(4, 13, 2));
		builder.add(this.date,							cc.xy(5, 15));
		builder.addLabel("Description: ", 				cc.xy(7, 11));			
		builder.add(new JScrollPane(this.description),	cc.xywh(8, 11, 2, 5));
		builder.add(this.submitButton,  		 		cc.xyw(8, 17,2));
		
		this.add(builder.getPanel());
		
		this.setVisible(true);
	}
	
	private void init(TroubleTicketController controller){

		this.userName = new  JTextField();
		this.caller = new JTextField();
		this.description = new JTextArea();
		this.description.setWrapStyleWord(true);
		this.description.setLineWrap(true);
		
		this.date = new JLabel();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		this.date.setText(format.format(new Date()));
		
		this.submitButton = new JButton("Submit Changes");
		this.submitButton.setActionCommand("Trouble_Ticket_change");
		this.submitButton.addActionListener(controller);
		this.submitButton.putClientProperty("UserName", this.userName);
		this.submitButton.putClientProperty("Caller", this.caller);
		this.submitButton.putClientProperty("Description", this.description);
		this.submitButton.putClientProperty("date", this.date);
			
	}
	
	public void updateSelectedView(TroubleTicket ticket){
		this.userName.setText(ticket.getUserName());
		this.caller.setText(ticket.getCallerName());
		this.date.setText(ticket.getDateTime());
		this.description.setText(ticket.getDescription());
	}

	@Override
	public void notifyChange() {
		// TODO Auto-generated method stub
		
	}

	
}
